<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Ruby basics</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Ruby, learn Ruby, basics, tutorial, programming language">
<meta name="description" content="In this part of the Ruby tutorial, we cover the basics of Ruby.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

</head>

<body>

<div class="container2">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content2">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>



<h1>Basics</h1>

<p>
In this part of the Ruby tutorial, we will cover basic programming concepts
of the Ruby language. We introduce the very basic programs. We will work with variables, 
constants and basic data types. We will read and write to the console; we will mention
variable interpolation.
</p>

<div class="big_hor">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* big_horizontal */
google_ad_slot = "2904953388";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
We start with a very simple code example. 
</p>

<pre class="code">
#!/usr/bin/ruby

puts "This is Ruby"
</pre>

<p>
This is simple Ruby script. It will print "This is Ruby" message to
the console. 
</p>

<pre class="explanation">
#!/usr/bin/ruby
</pre>

<p>
This is a path to the Ruby interpreter, which will execute the 
script. 
</p>

<pre class="explanation">
puts "This is Ruby"
</pre>

<p>
The <code>puts</code> is a Ruby keyword, which prints its argument to
the terminal. In our case the argument is a string message, delimeted
by double qoutes. 
</p>


<pre>
$ ./first.rb 
This is Ruby
</pre>

<p>
Executing the script gives the above output.
</p>

<hr class="btm">

<p>
We can read values from the terminal. 
(Terminal and console are synonyms)
</p>

<pre class="code">
#!/usr/bin/ruby

print "What is your name? "
name = gets

puts "Hello #{name}"
</pre>

<p>
The second program will read a value from a console
and print it. 
</p>

<pre class="explanation">
print "What is your name? "
</pre>

<p>
The <code>print</code> keyword prints a message to the console. 
The difference between the print and puts keywords is, that the print
keyword does not start a new line. The <code>puts</code> keyword
automatically starts a new line. 
</p>

<pre class="explanation">
name = gets
</pre>

<p>
Here we read an input from the user and store it in the name variable.
The <code>gets</code> is a method, which in our case reads a line
from the terminal. It is one of the methods, that we have at our
disposal by default. 
</p>


<pre class="explanation">
puts "Hello #{name}"
</pre>

<p>
In this code line, we perform variable interpolation. <b>Variable interpolation</b>
is replacing variables with their values inside string literals. 
Another names for variable interpolation are: variable substitution
and variable expansion.
</p>

<pre>
$ ./name.rb 
What is your name? Jan
Hello Jan
</pre>

<p>
This is the output of the second program.
</p>

<hr class="btm">

<p>
Ruby code can be run from the command line. This is inspired
by Perl one-liners, where small fragments of code are run to
do some petit tasks. 
</p>

<pre>
$ ruby -e "puts RUBY_VERSION"
1.9.3
</pre>

<p>
The -e option tells Ruby to execute Ruby code specified on the line and 
not to search for a Ruby file name. Our example prints the version of the
Ruby interpreter to the terminal.
</p>

<hr class="btm">

<p>
Ruby interpreter has a -c option, which checks the syntax of the code.
If this option is used, the code is not executed. If there is no syntax error,
Ruby will print "Syntax OK" to the standard output.  
</p>

<pre class="code">
#!/usr/bin/ruby

class Being end
    
m = Test.new
p m
</pre>

<p>
In the above example, there is a syntax error. If we put class and
end keywords on one line, we must also use the semicolon ; character.
</p>

<pre>
$ ruby -c syntax_check.rb 
syntax_check.rb:3: syntax error, unexpected keyword_end, expecting '&lt;' or ';' or '\n'
syntax_check.rb:6: syntax error, unexpected $end, expecting keyword_end
</pre>

<p>
Syntax error was found. If we put a semicolon after the Being class, the error
message will disappear.
</p>


<h2>Command line arguments</h2>

<p>
Ruby programs can receive command line arguments. They
follow the name of the program, when we run it. 
</p>

<pre class="code">
#!/usr/bin/ruby

puts ARGV
</pre>

<p>
Command line arguments specified after the
file name are available to a Ruby program in 
the global array named <code>ARGV</code>.
</p>

<pre class="explanation">
puts ARGV
</pre>

<p>
Here we print all the command line arguments to 
the terminal. 
</p>


<pre>
$ ./args.rb 1 2 3
1
2
3
</pre>

<p>
We provide three numbers as command line arguments and these are
printed to the console. 
</p>

<hr class="btm">

<p>
In the following example, we wil print all arguments and also
the script name. 
</p>

<pre class="code">
#!/usr/bin/ruby

puts $0
puts $*
</pre>

<p>
The $0 global variable contains the name of the script being executed.
Global variables in Ruby begin with the $ character. The $* is another
global variable. It is a synonym for the ARGV variable. It contains
command line arguments given for the current script. 
</p>

<pre>
$ ./args2.rb Ruby Python Perl
./args2.rb
Ruby
Python
Perl
</pre>

<p>
The args2.rb script receives three strings. The name of
the scrip and the three arguments are printed to the
terminal.
</p>


<h2>Variables and constants</h2>

<p>
A <b>variable</b> is a place to store data. A variable
has a name and a data type. Data types are different types
of values. For example integers, strings or floating point numbers.
Ruby is a dynamic language. This means, that we do not have to (and cannot) 
declare a variable to be of a certain data type. Instead, the Ruby
interpreter determines the data type at the moment of the assignment. 
Plus a variable can contain different values and also different
types of values over time. This differs from languages, which are strongly types,
Java, C or Pascal. 
Unlike variables, <b>constants</b> (should) retain their values. Once
initialized, they cannot be modified. In Ruby however, it is possible 
to change the value of a constant. In such a case a warning is issued. 
</p>

<pre class="code">
#!/usr/bin/ruby

city = "New York"
name = "Paul"; age = 35
nationality = "American"

puts city
puts name
puts age
puts nationality

city = "London"

puts city
</pre>

<p>
In the above example, we work with four variables. 
</p>

<pre class="explanation">
city = "New York"
</pre>

<p>
We assign a string value to the city variable. The variable
is dynamically created. 
</p>

<pre class="explanation">
name = "Paul"; age = 35
</pre>

<p>
We create two more variables.
We can put two statements into one line. But for
readability reasons, each statement should be rather
on a separate line. 
</p>

<pre class="explanation">
puts city
puts name
puts age
puts nationality
</pre>

<p>
We print the values of the variables to the terminal.
</p>

<pre class="explanation">
city = "London"
</pre>

<p>
We assign a new value to the city variable. 
</p>

<pre>
$ ./variables.rb 
New York
Paul
35
American
London
</pre>

<p>
Output of the example. 
</p>

<hr class="btm">

<p>
<b>Constants</b> As we already said above, constants store
one value over the time. Unlike in other languages, this rule is
however not enforced in Ruby. 
</p>

<pre class="code">
#!/usr/bin/ruby

WIDTH = 100
HEIGHT = 150

var = 40
puts var

var = 50
puts var

puts WIDTH
WIDTH = 110
puts WIDTH
</pre>

<p>
In this example, we declare two constants and one variable.
</p>

<pre class="explanation">
WIDTH = 100
HEIGHT = 150
</pre>

<p>
Constants in ruby begin with capital letter. It is a common practice
to write all characters in uppercase letters. 
</p>

<pre class="explanation">
var = 40
puts var

var = 50
</pre>

<p>
We declare and initialize a variable. Later, we assign a new value
to the variable. It is legal. 
</p>

<pre class="explanation">
WIDTH = 110
</pre>

<p>
We assign a new value to a constant. Constants should not be modified,
once they are created. Otherwise it has no meaning to create a constant.
The Ruby interpreter will issue a warning.
</p>

<pre>
$ ./constants.rb 
40
50
100
./constants.rb:13: warning: already initialized constant WIDTH
110
</pre>

<p>
Output of the script. 
</p>



<h2>Variable interpolation</h2>

<p>
<b>Variable interpolation</b> is replacing
variables with their values inside string literals.
Another names for variable interpolation are: variable 
substitution and variable expansion.
</p>

<pre class="code">
#!/usr/bin/ruby

age = 34
name = "William"

puts "#{name} is #{age} years old" 
</pre>

<p>
In Ruby, strings are immutable. We cannot modify an existing
string. Variable interpolation happens during string creation. 
</p>

<pre class="explanation">
age = 34
name = "William"
</pre>

<p>
Here we declare two variables. 
</p>

<pre class="explanation">
puts "#{name} is #{age} years old" 
</pre>

<p>
The string message has double quotes as its boundaries.
When we put a variable name between the #{ and } characters,
the variable is interpolated, that is replaced with its value.
</p>

<pre>
$ ./interpolation.rb 
William is 34 years old
</pre>

<p>
Output.
</p>

<p>
This chapter covered some basics of the Ruby language.
</p>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified December 14, 2011 <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

